package com.xhwl.logistics.config;


import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.utils.ThreadLocalUtil;

import com.xhwl.logistics.util.RedisUtils;
import com.xhwl.logistics.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * @Author: JCccc
 * @Description:
 * @Date: 2020/11/18 17:28
 */
@Component
public class WebAccountArgResolver implements WebArgumentResolver {
    private static final Logger log = LoggerFactory.getLogger(WebAccountArgResolver.class);

    private String ACCOUNT_SAVE_PREFIX = "USERINFO_";

    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) {
        if (null != methodParameter.getParameterType() && methodParameter.getParameterType().equals(Account.class)) {
            HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
            //来自网关解析token后，往请求头新增的userId
            String userId = request.getHeader("userId");
            String appId = request.getHeader("appId");
            RedisUtils redisUtils = SpringContextUtils.getBean(RedisUtils.class);
            Object accountInfo = redisUtils.get(appId + "_" + ACCOUNT_SAVE_PREFIX + userId);
            if (Objects.nonNull(accountInfo)) {
                Account account = conversionAccount((JSONObject) accountInfo);
                ThreadLocalUtil.setAccount(account);
                return account;
            }
            log.info("不存在当前账号的登录信息，userId={}", userId);
        }


        return UNRESOLVED;
    }


    /**
     * 转换自动注入的账号数据
     *
     * @param jsonObject
     * @return
     */
    private Account conversionAccount(JSONObject jsonObject) {
        Account account = new Account();
        account.setId((Integer) jsonObject.get("userId"));
        account.setIndustryId(jsonObject.getShort("industryId"));
        account.setLoginName(jsonObject.getString("loginName"));
        account.setPassword(jsonObject.getString("password"));
        account.setNickname(jsonObject.getString("nickName"));
        account.setName(jsonObject.getString("name"));
        account.setPhone(jsonObject.getString("phone"));
        account.setSex((Short) jsonObject.get("sex"));
        account.setAge(jsonObject.getInteger("age"));
        account.setFaceId(jsonObject.getString("faceId"));
        account.setFaceUrl(jsonObject.getString("url"));
        account.setStatus((Short) jsonObject.get("status"));
        account.setIsDeleted(jsonObject.getBoolean("isDeleted"));
        account.setUpdatedAt((Date) jsonObject.get("updatedAt"));
        account.setCreatedAt((Date) jsonObject.get("createdAt"));
        return account;
    }


}
